<?php

namespace helper;

use Firebase\JWT\JWT;

class JwtHandler
{

  /**
   * 加密
   * @param $payload
   * @param string $key
   * @param string $alg
   * @param null $keyId
   * @param null $head
   * @return string
   * @throws \Exception
   */
  public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $head = null)
  {
    return JWT::encode($payload, $key, $alg, $keyId, $head);
  }

  /**
   * 解密
   * @param $jwt
   * @param $key
   * @param array $allowed_algs
   * @return array
   * @throws \Exception
   */
  public static function decode($jwt, $key, array $allowed_algs = ["HS256"])
  {
    $payLoad = JWT::decode($jwt, $key, $allowed_algs);
    return (array)$payLoad;
  }

}